package setting

import (
	"context"
	"strings"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/forum/rpc/forumclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type SetPageSettingLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSetPageSettingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetPageSettingLogic {
	return &SetPageSettingLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SetPageSettingLogic) SetPageSetting(req *types.PageSettingReq) (resp *types.BaseMsgResp, err error) {
	if req.Key == nil || len(strings.TrimSpace(*req.Key)) == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	if req.Value == nil || len(strings.TrimSpace(*req.Value)) == 0 {
		return nil, errorx.NewInvalidArgumentError(i18n.ValidationError)
	}

	_, err = l.svcCtx.ForumRpc.BackendSetPageSetting(l.ctx, &forumclient.BackendSetPageSettingReq{
		Key:     req.Key,
		Setting: req.Value,
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Success: true,
		Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
	}, nil
}
